package com.rst.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Classe;
import com.rst.service.ClassService;

@Controller
public class ClassController {

	@Autowired
	ClassService classService;
	
	@RequestMapping(value="/addClass", method=RequestMethod.GET)
	public ModelAndView addClass()
	{
		ModelAndView mav = new ModelAndView("class/createClass");
		Classe clas = new Classe();
		mav.getModelMap().put("class", clas);
		return mav;
	}
	
	@RequestMapping(value="/saveClass", method=RequestMethod.POST)
	public String saveClass(@ModelAttribute("class")Classe clas, BindingResult result, SessionStatus status)
	{
		Long id = classService.save(clas);
		status.setComplete();
		return "redirect:manageclass.htm";
	}
	
	@RequestMapping(value="/editClass", method=RequestMethod.GET)
	public ModelAndView editClass(@RequestParam("id")Long classId , HttpServletRequest request)
	{
		ModelAndView mav = new ModelAndView("class/editClass");
		Classe class1 = classService.getClassById(classId);
		mav.addObject("editClass", class1);
		return mav;
	}
	
	@RequestMapping(value="/updateClass", method=RequestMethod.POST)
	public String updateClass(@ModelAttribute("updateClass") Classe class1,SessionStatus status,
			@RequestParam("id")Long classId)
	{
		classService.update(class1,classId);	
		status.setComplete();
		return "redirect:manageclass.htm";
	}
	@RequestMapping(value="/classAndSection", method=RequestMethod.GET)
	public ModelAndView classAndSection()
	{
		ModelAndView mav = new ModelAndView("class/classAndSection");
		return mav;
	}
	
	@RequestMapping(value="/manageclass", method=RequestMethod.GET)
	public ModelAndView manageClass()
	{
		ModelAndView mav = new ModelAndView("class/manageClass");
		List<Classe> classList = classService.getClassList();
		mav.addObject("classList", classList);
		return mav;
	}
	@RequestMapping(value="/deleteClass", method=RequestMethod.GET)
	public String deleteClass(@RequestParam("id")Long classId , HttpServletRequest request)
	{
		String status = classService.deleteClassById(classId);
		return "redirect:manageclass.htm";
	}


}
